package com.windfone.web;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

/**
 * Session监听器
 * 
 * @author PF
 * 
 */
public class SessionListener implements HttpSessionListener {

	// Session集合

	public static Map<String, HttpSession> session_map = new HashMap<String, HttpSession>();

	/**
	 * 创建一个session
	 */
	public void sessionCreated(HttpSessionEvent sessionEvent) {
		String session_id = sessionEvent.getSession().getId();
		System.out.println("======创建了一个Session,id是:" + session_id + "======");
		session_map.put(session_id, sessionEvent.getSession());
	}

	/**
	 * 销毁
	 */
	public void sessionDestroyed(HttpSessionEvent sessionEvent) {
		session_map.remove(sessionEvent.getSession());
	}

	/**
	 * 获得session
	 * 
	 * @param session_id
	 * @return
	 */
	public static HttpSession getSessionById(String session_id) {
		return session_map.get(session_id);
	}

	/**
	 * 移除session
	 * 
	 * @param session_id
	 */
	public static void removeSessionById(String session_id) {
		session_map.remove(session_id);
	}

}
